package main

import (
	"context"
	v1 "demo01/vector-test/proto"
	"fmt"
	milvusClient "github.com/milvus-io/milvus-sdk-go/v2/client"
	"github.com/milvus-io/milvus-sdk-go/v2/entity"
	"google.golang.org/grpc"
	"log"
)

const imageUrl = "http://10.10.22.172:9092/fapi/file/eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmaWxlX3R5cGUiOiJhZGRpdGlvbmFsIiwidXNlcl9pZCI6MTA0MywiZmlsZV9uYW1lX25vX2V4dCI6ImNtRjViemMyTGw5RGNtVmhkR1ZmWVY5c2IyZHZYMlp2Y2w5aFgySnRlRjl5WVdOcGJtZGZjR2h2ZEc5bmNtRndhSGxmWW5WemFXNWxjM05mWkdKaU9ESXhZbU10TkRoaVl5MDBNbVV4TFdFMlptSXRabVk1TmpBeU1USmxOMlZoTFQ0eE56STNNalUwT1RBME16UTNOVEl4T1RVMCIsImZpbGVfZXh0X29yaWdpbmFsIjoiLnBuZyIsImZpbGVfZXh0X2NvbXByZXNzIjoiLndlYnAiLCJmaWxlX3RlbXBsYXRlIjoiaW1hZ2UifQ.Uz0-yGmysfx52DH0-y3YrxCOIkt_8VB1I5LHPiWCR3NCIOx5zf_bxPWudee4khdvCcBmyAR_q1kBrGkN13zXZo7qyrsFNDExD17V-xOq6wx6cVocSfOk32ZjnsE78f6nuAp95ACPWn3MZMfwd1oziszuYME38DjCQbo9waEfdSORJ94Trto4yd4RBIWAy7Icc_8XtEroRxRYHYdM6fNHTiNQb6ySHTQuQmcGZkwWPNwlriSYNlKQTFLgdz76oQPuqUcsDrNjGIib2VH0EmsGj7o6hsBEvmNCXf0ZiM3ZJoH6AZ-P4Gc71rlv5DhaNb-O1jDMd4FSyduv5lzaWPWCHw?token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ4ZC1kZXNpZ24iLCJleHAiOjE3Mjc4NTk2MTEsImlhdCI6MTcyNzI1NDgxMSwiaXNzIjoieGQtbWlkam91cm5leS1zZXJ2ZXIiLCJUb2tlblBvbGljeSI6eyIxIjpbeyJQZXJtaXNzaW9uIjoxLCJGaWxlRGlyIjoiL2FkZGl0aW9uYWwvMTA0My9vcmlnaW5hbC8ifV0sIjIiOlt7IlBlcm1pc3Npb24iOjEsIkZpbGVEaXIiOiIvcHVibGljLyovKi8ifSx7IlBlcm1pc3Npb24iOjEsIkZpbGVEaXIiOiIvKi8xMDQzLyovIn0seyJQZXJtaXNzaW9uIjoxLCJGaWxlRGlyIjoiLyovKi8qLyJ9LHsiUGVybWlzc2lvbiI6MSwiRmlsZURpciI6Ii8qLyovd2ViX2xvc3NsZXNzLyJ9LHsiUGVybWlzc2lvbiI6MSwiRmlsZURpciI6Ii8qLyovd2ViX2hpZ2gvIn0seyJQZXJtaXNzaW9uIjoxLCJGaWxlRGlyIjoiLyovKi93ZWJfbWlkZGxlLyJ9LHsiUGVybWlzc2lvbiI6MSwiRmlsZURpciI6Ii8qLyovd2ViX2xvdy8ifV19fQ.n3khYpJz-PVnb-EW0zaX6GBZ3oe0q2QCQEl1RDegd8-by_mboy32yolrYiaF2U4kTyBpL3sOKnlf0Qq5ZfgcK0rgzKwlBsvFSpyCZ06a9kP3JF14Dh39s9mwou_HDpLpiq8hNZoaBb3MIft3HjDMvlLJY2fGODaAg9lhNM9ENP-sTBUBKNcZ5gzN6zEURZbenOe3X2pvg-s3Dr8VhEcCe10zzDDd_J_YtFMXinHfmaRlg1x-ib_TCxBB4BQNGcqM-Xsia53m-lb9N_WjT2SR5b9-pOSVGvItbPA7-kzEiy7-k090_td2menllXsn4ifw2lH8sySZtWDtsTu1kpZ_CQ&file_quality=web_lossless"

func main() {
	// 连接到gRPC服务器
	conn, err := grpc.Dial("10.10.22.104:50051", grpc.WithInsecure())
	if err != nil {
		log.Fatalf("无法连接到服务器：%v", err)
	}
	defer conn.Close()
	// 创建gRPC客户端
	client := v1.NewGreeterClient(conn)

	//准备gRPC请求
	req := &v1.EmbeddingRequest{Url: imageUrl}
	// 发送gRPC请求
	resp, err := client.UploadImageEmbedding(context.Background(), req)
	if err != nil {
		log.Fatalf("UploadImageEmbedding：%v", err)
	}
	// 处理响应
	log.Println(resp)
	ctx := context.Background()
	milvusCli, err := milvusClient.NewGrpcClient(
		ctx,                  // ctx
		"10.10.22.104:19530", // addr
	)
	if err != nil {
		log.Fatal("failed to connect to Milvus:", err.Error())
	}

	// 2. 提取待查询图片的 embedding (假设 embedding 是一个 float32 列表)
	queryEmbedding := resp.Vectors // 128 维或其他维度的向量

	collectionName := "gallery2"
	var limit = 10

	searchParams, err := entity.NewIndexIvfFlatSearchParam(limit)
	if err != nil {
		fmt.Println("Error creating search parameters:", err)
		return
	}
	searchResult, err := milvusCli.Search(
		ctx,
		collectionName,
		[]string{}, // 这里可以指定搜索条件，例如过滤条件
		"",
		[]string{"gallery_image_id"}, // 返回的 ID 列
		[]entity.Vector{entity.FloatVector(queryEmbedding)}, // 查询的向量
		"embedding",   // 向量字段名称
		entity.COSINE, // 相似度计算方式
		limit,         // 返回的结果数量
		searchParams,  // 搜索参数
		milvusClient.WithSearchQueryConsistencyLevel(entity.ClBounded),
	)
	if err != nil {
		log.Fatal("failed to connect to Milvus:", err.Error())
	}

	fmt.Println(searchResult)

}
